package com.sandmens.droidvdr;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.KeyEvent;

public class Preferences extends PreferenceActivity {
	private static final String PREFS_NAME = "droidVDRsettings";
	
	private Preference nw_setting_1;
	private Preference check_nw1;
	private boolean isNwActive1 = false;
	private CheckBoxPreference checkbox_nw1;

	private Preference nw_setting_2;
	private Preference check_nw2;
	private boolean isNwActive2 = false;
	private CheckBoxPreference checkbox_nw2;
	
	private Preference check_auto_speed;
	private boolean isAutoSpeedActive = false;
	private CheckBoxPreference checkbox_check_auto_speed;

	private Preference use_camera_player;
	private boolean useCameraPlayer = false;
	private CheckBoxPreference checkbox_check_camera_player;

	private Preference use_utf8;
	private boolean useutf8 = false;
	private CheckBoxPreference checkbox_check_use_utf8;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.preferences);
		ReadSettings();
		
		// Checkbox NW 1
		check_nw1 = (Preference) findPreference("checkboxNetwork1");
		checkbox_nw1 = (CheckBoxPreference) check_nw1;
		if ( isNwActive1 )
			checkbox_nw1.setChecked(true);
		else
			checkbox_nw1.setChecked(false);
		
		check_nw1.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			if ( checkbox_nw1.isChecked())
			{
				nw_setting_1.setEnabled(true);
				if (checkbox_nw2 != null)
				{
					checkbox_nw2.setChecked(false);
				}
				if ( nw_setting_2 != null)
				{
					nw_setting_2.setEnabled(false);
				}
			}
			else
			{
				nw_setting_1.setEnabled(false);
			}
			return false;
		}
		});

		//Edit Settings NW 1
		nw_setting_1 = (Preference) findPreference("nw1_settingPref");
		if ( isNwActive1 )
			nw_setting_1.setEnabled(true);
		else
			nw_setting_1.setEnabled(false);
		nw_setting_1.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			Intent int_setting_nw1 = new Intent();
			int_setting_nw1.setClass(getApplicationContext(),settingNetwork1.class);
			startActivityForResult(int_setting_nw1,0);
			return true;
		}
		});
		
		
		// Checkbox NW 2
		check_nw2 = (Preference) findPreference("checkboxNetwork2");
		checkbox_nw2 = (CheckBoxPreference) check_nw2;
		if ( isNwActive2 )
			checkbox_nw2.setChecked(true);
		else
			checkbox_nw2.setChecked(false);
		
		check_nw2.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			if ( checkbox_nw2.isChecked())
			{
				nw_setting_2.setEnabled(true);
				if (checkbox_nw1 != null)
				{
					checkbox_nw1.setChecked(false);
				}
				if ( nw_setting_1 != null)
				{
					nw_setting_1.setEnabled(false);
				}
			}
			else
			{
				nw_setting_2.setEnabled(false);
			}
			return false;
		}
		});

		//Edit Settings NW 1
		nw_setting_2 = (Preference) findPreference("nw2_settingPref");
		if ( isNwActive2 )
			nw_setting_2.setEnabled(true);
		else
			nw_setting_2.setEnabled(false);
		nw_setting_2.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			Intent int_setting_nw2 = new Intent();
			int_setting_nw2.setClass(getApplicationContext(),settingNetwork2.class);
			startActivityForResult(int_setting_nw2,1);
			return true;
		}
		});

		
		// Checkbox Auto Speed
		check_auto_speed = (Preference) findPreference("checkboxAuto");
		if ( check_auto_speed != null)
		{
			checkbox_check_auto_speed = (CheckBoxPreference) check_auto_speed;
			if ( isAutoSpeedActive )
				checkbox_check_auto_speed.setChecked(true);
			else
				checkbox_check_auto_speed.setChecked(false);
		}
		checkbox_check_auto_speed.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			return false;
		}
		});
		


		// Checkbox Use Camera Player
		use_camera_player = (Preference) findPreference("checkboxCameraPlayer");
		if ( use_camera_player != null)
		{
			checkbox_check_camera_player = (CheckBoxPreference) use_camera_player;
			if ( useCameraPlayer )
				checkbox_check_camera_player.setChecked(true);
			else
				checkbox_check_camera_player.setChecked(false);
		}
		checkbox_check_camera_player.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			return false;
		}
		});
		
		// Checkbox Use UTF8
		use_utf8 = (Preference) findPreference("checkboxUseUTF8");
		if ( use_utf8 != null)
		{
			checkbox_check_use_utf8 = (CheckBoxPreference) use_utf8;
			if ( useutf8 )
				checkbox_check_use_utf8.setChecked(true);
			else
				checkbox_check_use_utf8.setChecked(false);
		}
		checkbox_check_use_utf8.setOnPreferenceClickListener(new OnPreferenceClickListener() 
		{
		public boolean onPreferenceClick(Preference preference) 
		{
			return false;
		}
		});

		
	}
	
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    } 

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if ( keyCode == KeyEvent.KEYCODE_BACK)
		{
        	Bundle resultBundle = new Bundle();
        	resultBundle.putString("ACTION", "RELOAD_SETTINGS");
        	Intent intent = new Intent();
        	intent.putExtras(resultBundle);
        	save_Setting();
        	setResult(RESULT_OK, intent);
			finish();
		}			
		return super.onKeyDown(keyCode, event);
	}

    private void save_Setting()
    {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        if ( checkbox_nw1.isChecked())
        {
        	editor.putBoolean("network1_active", true);
        	editor.putBoolean("network2_active", false);
        }
        if ( checkbox_nw2.isChecked())
        {
        	editor.putBoolean("network1_active", false);
        	editor.putBoolean("network2_active", true);
        }
        if ( checkbox_check_auto_speed.isChecked())
        {
        	editor.putBoolean("network_use_auto", true);
        }
        else
        {
        	editor.putBoolean("network_use_auto", false);
        }
       
        if ( checkbox_check_camera_player.isChecked())
        {
        	editor.putBoolean("use_camera_player", true);
        }
        else
        {
        	editor.putBoolean("use_camera_player", false);
        }

        if ( checkbox_check_use_utf8.isChecked())
        {
        	editor.putBoolean("use_utf8", true);
        }
        else
        {
        	editor.putBoolean("use_utf8", false);
        }
        
        editor.commit();
    }
	
  	private void ReadSettings()
  	{
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        isNwActive1 = settings.getBoolean("network1_active", false);
        isNwActive2 = settings.getBoolean("network2_active", false);
        isAutoSpeedActive = settings.getBoolean("network_use_auto", false);
        useCameraPlayer = settings.getBoolean("use_camera_player", false);
        useutf8 = settings.getBoolean("use_utf8", false);
  	}

}
